package www.bawei.springboot.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import www.bawei.springboot.common.RedisKeyManager;
import www.bawei.springboot.domain.User;
import www.bawei.springboot.service.UserService;
import www.bawei.springboot.util.jsonUtils;
@Service
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {

	@Autowired
	private RedisTemplate redisTemplate;
	@Override
	public void addUser(final User user) {
		final Long uid=getId();
		user.setUid(uid);
		redisTemplate.execute(new RedisCallback<User>() {
			byte[] key=redisTemplate.getStringSerializer().serialize(String.format(RedisKeyManager.MODEL_KEY, User.class.getName(),uid.toString()));
			byte[] value=redisTemplate.getValueSerializer().serialize(user);
			byte[] keyAll=redisTemplate.getStringSerializer().serialize(String.format(RedisKeyManager.MODEL_ALL,User.class.getName()));
			@Override
			public User doInRedis(RedisConnection connection) throws DataAccessException {
				connection.set(key, value);
				connection.zAdd(keyAll, uid,key);
				return null;
			}
		});
	}

	@Override
	public void delUser(final Long uid) {
		redisTemplate.execute(new RedisCallback<Long>() {
			byte[] key=redisTemplate.getStringSerializer().serialize(String.format(RedisKeyManager.MODEL_KEY, User.class.getName(),uid.toString()));
			
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				User user = (User) redisTemplate.getValueSerializer().deserialize(connection.get(key));
				byte[] keyAll=redisTemplate.getValueSerializer().serialize(String.format(RedisKeyManager.MODEL_ALL,User.class.getName()));
				connection.del(key);
				connection.zRem(keyAll,key);
				return null;
			}
		});
	}

	@Override
	public void updUser(final User user) {
		redisTemplate.execute(new RedisCallback<User>() {
			byte[] key=redisTemplate.getStringSerializer().serialize(String.format(RedisKeyManager.MODEL_KEY, User.class.getName(),user.getUid().toString()));
			byte[] value=redisTemplate.getValueSerializer().serialize(user);
			@Override
			public User doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				connection.set(key, value);
				return null;
			}
		});
	}
	
	
	@Override
	public List<User> getUser() {
		final List<User> list=new ArrayList<User>();
		redisTemplate.execute(new RedisCallback<List>() {
			byte[] keyAll=redisTemplate.getStringSerializer().serialize(String.format(RedisKeyManager.MODEL_ALL,User.class.getName()));
			@Override
			public List doInRedis(RedisConnection connection) throws DataAccessException {
				Set<byte[]> set = connection.zRange(keyAll, 0,-1);
				for (byte[] s : set) {
					
					list.add((User) redisTemplate.getValueSerializer().deserialize(connection.get(s)));
				}
				return null;
			}
		});
		return list;
	}

	@Override
	public User getUserById(final Long uid) {
		
		return (User) redisTemplate.execute(new RedisCallback<User>() {
			byte[] key=redisTemplate.getStringSerializer().serialize(String.format(RedisKeyManager.MODEL_KEY,User.class.getName(),uid.toString()));
			@Override
			public User doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				User user = (User) redisTemplate.getValueSerializer().deserialize(connection.get(key));
				return  user;
			}
		});
		
	}

	@Override
	public Long getId() {
		final String key=String.format(RedisKeyManager.MODEL_ID,User.class.getName());
		return (Long) redisTemplate.execute(new RedisCallback<Long>() {
			@Override
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				// TODO Auto-generated method stub
				return connection.incr(key.getBytes());
			}
		});
		
	}
	

}
